package belajarzk.action.maintenance.helper;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treecol;
import org.zkoss.zul.Treecols;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Treerow;

import belajarzk.common.Common;
import belajarzk.database.hibernate.HibernateUtil;
import belajarzk.database.model.Menu;
import belajarzk.database.model.Tbmrole;
import belajarzk.ui.render.grid.TbmroleRenderer;
import belajarzk.ui.util.MyTolbarbutton;


public class TbmroleActionHelper {

	private synchronized static Boolean hasChild(Integer root, Set<Menu> menus) {
		for (Menu menu : menus) {
			if (menu.getRoot().equals(root)) {
				return true;
			}
		}
		return false;
	}

	private synchronized static void createTreerow(final Tree tree,
			final Groupbox groupbox, Treeitem treeitem, final Menu menu,
			final Tbmrole job) {
		Treerow treerow = new Treerow();
		new Treecell(menu.getLabel()).setParent(treerow);

		Treecell treecell = new Treecell();
		Hbox hbox = new Hbox();
		hbox.setHeight("40px");
		MyTolbarbutton button = new MyTolbarbutton(Common
				.getBahasa("common_delete"), "/img/delete.gif");
		button.setOrient("vertical");
		button.addEventListener("onClick", new EventListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void onEvent(Event arg0) throws Exception {
				// TODO Auto-generated method stub
				Messagebox.show("Apakah anda ingin menghapus menu ini ?",
						Common.getBahasa("question"), Messagebox.OK
								| Messagebox.CANCEL, Messagebox.EXCLAMATION,
						new EventListener() {

							@Override
							public void onEvent(Event event) throws Exception {
								int i = new Integer(event.getData().toString());
								if (i == Messagebox.OK) {
									job.getMenus().remove(menu);
									Session session = HibernateUtil
											.currentSession();
									try {

										session.update(session.merge(job));

										List list = groupbox.getChildren();
										((Component) list.get(0)).detach();
										((Component) list.get(0)).detach();

										final Tree mytree = TbmroleRenderer
												.createTree(job, groupbox);
										mytree.setVisible(true);
										TbmroleActionHelper.createTreeMenu(
												groupbox, mytree, job);

									} catch (Exception e) {
										e.printStackTrace();

										Messagebox
												.show("Data ini tidak bisa dirubah ..");
									}

								}

							}
						});

			}

		});
		button.setParent(hbox);
		hbox.setParent(treecell);
		treecell.setParent(treerow);
		treerow.setParent(treeitem);
	}

	private synchronized static void createRootSubMenu(Tree tree,
			final Groupbox groupbox, Integer root, Set<Menu> menus,
			Component componen, Tbmrole job, Set<Integer> check) {
		Treechildren tc1 = new Treechildren();
		for (final Menu menu : menus) {
			if (menu.getRoot().equals(root) && !check.contains(menu.getId())) {
				check.add(menu.getId());
				Boolean ada = hasChild(menu.getChild(), menus);
				if (ada) {
					Treeitem treeitem = new Treeitem();
					treeitem.setValue(menu.getUrl());
					createTreerow(tree, groupbox, treeitem, menu, job);
					treeitem.setParent(tc1);
					createRootSubMenu(tree, groupbox, menu.getChild(), menus,
							treeitem, job, check);
				} else {
					Treeitem treeitem = new Treeitem();
					treeitem.setValue(menu.getUrl());
					createTreerow(tree, groupbox, treeitem, menu, job);
					treeitem.setParent(tc1);
				}
			}
		}

		tc1.setParent(componen);
	}

	public synchronized static void createTreeMenu(final Groupbox groupbox,
			Tree tree, Tbmrole job) {
		Set<Integer> check = new HashSet<Integer>();
		Treecols treecols = new Treecols();
		Treecol treecol = new Treecol("");
		treecol.setWidth("95%");
		treecol.setParent(treecols);
		treecol = new Treecol("");
		treecol.setWidth("5%");
		treecol.setParent(treecols);
		treecols.setParent(tree);

		final Set<Menu> menus = job.getMenus();
		Treechildren tc1 = new Treechildren();
		for (final Menu menu : menus) {
			if (menu.getRoot().equals(0) && !check.contains(menu.getId())) {
				check.add(menu.getId());
				System.out.println("menu = " + menu.getLabel());
				Treeitem treeitem = new Treeitem();
				treeitem.setValue(menu.getUrl());
				createTreerow(tree, groupbox, treeitem, menu, job);
				treeitem.setParent(tc1);
				createRootSubMenu(tree, groupbox, menu.getChild(), menus,
						treeitem, job, check);
			}
		}
		tc1.setParent(tree);
	}

}
